<script>
export default {
	onLaunch: function (options) {
		console.log('App Launch');
		let that = this;
		let share_id = options.query.share_id ? options.query.share_id : '';
		let scene = decodeURIComponent(options.query.scene);
		if (share_id.length == 0 && scene) {
			share_id = scene;
		}
		if (share_id.length > 0) {
			that.globalData.share_id = share_id;
		}

		uni.login({
			provider: 'weixin', // 使用微信登录
			success: (res) => {
				console.log('获取 OpenID 成功', { code: res.code });
				let systemInfo = uni.getSystemInfoSync();
				let data = {
					ac: 'getopenid',
					code: res.code,
					miniapp_id: 1,
					device: systemInfo.brand + ' ' + systemInfo.model,
					share_id: that.globalData.share_id || ''
				};
				uni.request({
					url: 'https://yc.aiaud.cn/api/Index',
					data,
					header: {
						'Content-Type': 'application/x-www-form-urlencoded'
					},
					method: 'POST',
					success: (response) => {
						let resdata = response.data;
						if (resdata && resdata.data) {
							console.log('服务器返回用户数据:', resdata.data);
							uni.$u.vuex('vuex_openid', resdata.data.openid);
							uni.$u.vuex('vuex_user', resdata.data);
							uni.$u.mpShare.title = '睡前英语有声听书音频库';
							uni.$u.mpShare.path = 'pages/index/index?share_id=' + resdata.data.data.invite_id;
						} else {
							console.error('无效的服务器响应:', response);
						}
					},
					fail: (err) => {
						console.error('Request failed', err);
					}
				});
			},
			fail: (err) => {
				console.error('wx.login failed', err);
			}
		});
		if (uni.canIUse('getUpdateManager')) {
			const updateManager = uni.getUpdateManager();
			updateManager.onCheckForUpdate(function (res) {
				if (res.hasUpdate) {
					updateManager.onUpdateReady(function () {
						uni.showModal({
							title: '更新提示',
							content: '新版本已经准备好，是否重启应用？',
							success: function (res) {
								if (res.confirm) {
									updateManager.applyUpdate();
								}
							}
						});
					});
					updateManager.onUpdateFailed(function () {
						uni.showModal({
							title: '已经有新版本了哟~',
							content: '新版本已经上线啦~，请您删除当前小程序，重新搜索打开哟~'
						});
					});
				}
			});
		}
	},
	onShow: function () {
		console.log('App Show');
	},
	onHide: function () {
		console.log('App Hide');
	}
};
</script>

<style lang="scss">
/* 注意要写在第一行，同时给style标签加入lang="scss"属性 */
@import '@/uni_modules/uview-ui/index.scss';
</style>
